﻿using System;
using System.Collections.Generic;
using System.Linq;
using Entalpy.dto;

namespace Entalpy.Model
{
   public class BasisSetter : IBasisSetter
    {
        public double GetBasis(IList<Data> Data)
        {
            IDictionary<double, int> Gis = GetGistogramData(Data);

            int maxEntriesCount = Gis.Max((pair) => { return pair.Value; });
            double basis = Gis.First((kpair) => { return kpair.Value == maxEntriesCount; }).Key;
            return basis;
           
        }

        public IDictionary<double, int> GetGistogramData(IList<Data> Data)
        {
            IDictionary<double, int> Gis = new Dictionary<double, int>();

            foreach (var D in Data)
            {
                if (Gis.ContainsKey(D.Intensity))
                {
                    Gis[D.Intensity]++;
                }
                else
                {
                    Gis.Add(D.Intensity, 1);
                }
            }

            return Gis;
        }
       
        public IList<Data> GetNewData(IList<Data> Data)
        {
            double Basis = GetBasis(Data);

            foreach (Data d in Data)
            {
                d.Intensity -= Basis;
            }

            return Data;
        }
    }
}
